<?php


        的
namespace api\project\middleware;


use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class CheckToken
{
    public function handle($request, \Closure $next)
    {
        $token = $request->header();
        if(!isset($token['token']) || empty($token['token']))
        {
            return json(['code'=>0,'msg'=>'code不能为为空']);
        }
        $key = new Key(config('app.jwt_key'),'HS256');

        try {
            $res = JWT::decode($token['token'],$key);
        }catch (\Exception $exception){
            return json(['code'=>0,'msg'=>$exception->getMessage()]);
        }
        $request->user_id = $res->data->user_id;
        return $next($request);
    }
}